﻿var maphieuyeucauxuatvattu = "", maphieuyeucauxuatvattu_chitiet = "";
$(function () {
    $("#ddlKho_Khoa").parent().find(".dropdown-toggle").focus();
    $("#txtTuNgay").data("DateTimePicker").setMaxDate($("#txtDenNgay").val());
    $("#txtDenNgay").data("DateTimePicker").setMinDate($("#txtTuNgay").val());
    // Change value cho textbox từ ngày, đến ngày
    $("#txtTuNgay").on("dp.change", function (e) {
        $("#txtDenNgay").data("DateTimePicker").setMinDate(e.date);
    });
    $("#txtDenNgay").on("dp.change", function (e) {
        $("#txtTuNgay").data("DateTimePicker").setMaxDate(e.date);
    });
    // Enter của textbox đến ngày
    $("#txtDenNgay").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnTimKiem").focus();
        }
    });
    // Event khi đóng modal bootstrap
    $("#div_phieuyeucauxuatvattu").on("hide", function () {
        window.__doPostBack("btnTimKiem_Click", "");
    });
    // Enter của textbox ghi chú
    $("#txtDienGiai").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnLuuPhieu").focus();
        }
    });
    // Enter của textbox số lượng
    $("#txtSoLuong").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnLuuPhieu_ChiTiet").focus();
        }
    });
    // Event change combobox vật tư
    $("#ddlVatTu").on("change", function () {
        var data = SelectVatTuById($(this).val());
        if (data.length <= 0) return;
        $("#ddDonVi").selectpicker("val", data[0].MaDonVi);
        $("#ddDonVi").selectpicker("refresh");
        $("#txtSoLuong").focus();
    });
    // Change tab
    $("#ul_tab_phieuyeucauxuatvattu a[href=\"#tab_phieuyeucauxuatvattu\"]").on("shown.bs.tab", function (e) {
        $("#txtNguoiYeuCau").focus();
    });
    $("#ul_tab_phieuyeucauxuatvattu a[href=\"#tab_phieuyeucauxuatvattu_chitiet\"]").on("shown.bs.tab", function (e) {
        $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
    });
});
function loadcombobox() {
    filldropdown("#ddlKhoYeuCau", superadmin === true ? SelectAllKho() : SelectKhoByKhoa(makhoa));
    filldropdown("#ddlKhoXuat", superadmin === true ? [] : PhieuYeuCauXuatVatTu_SelectKhoXuat(makhoa));
    filldropdown("#ddlVatTu", SelectAllVatTu());
    filldropdown("#ddDonVi", SelectAllDonVi());
    $("#ddDonVi").find("[value=\"\"]").text("");
    $("#ddDonVi").selectpicker("refresh");
}
function validationform_Timkiem() {
    $("#txtTuNgay").rules("add", {
        required: true,
        messages: {
            required: MsErrorEmpty
        }
    });
    $("#txtDenNgay").rules("add", {
        required: true,
        messages: {
            required: MsErrorEmpty
        }
    });
}
function themphieuyeucauxuatvattu() {
    if (CheckSession() === "") { window.location = "/"; return; }
    loadcombobox();
    $("#ul_tab_phieuyeucauxuatvattu a[href=\"#tab_phieuyeucauxuatvattu\"]").tab("show");
    loaddanhsachvattu_phieuyeucauxuatvattu(maphieuyeucauxuatvattu);
    $("#div_phieuyeucauxuatvattu").modal("show");
}
function suaphieuyeucauxuatvattu(tagButton) {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#btnNhapLaiPhieu").hide();
    loadcombobox();
    maphieuyeucauxuatvattu = $(tagButton).attr("maphieu");
    if (maphieuyeucauxuatvattu === "") return;
    var dataPhieuYeuCauXuatVatTu = PhieuYeuCauXuatVatTu_SelectById(maphieuyeucauxuatvattu);
    (dataPhieuYeuCauXuatVatTu[0].NgayYeuCau == null || dataPhieuYeuCauXuatVatTu[0].NgayYeuCau === "")
        ? $("#txtNgayYeuCau").val("")
        : $("#txtNgayYeuCau").data("DateTimePicker").setDate(moment(dataPhieuYeuCauXuatVatTu[0].NgayYeuCau).format("DD-MM-YYYY"));
    $("#txtNguoiYeuCau").val(dataPhieuYeuCauXuatVatTu[0].NguoiYeuCau);
    $("#ddlKhoYeuCau").selectpicker("val", dataPhieuYeuCauXuatVatTu[0].MaKhoYeuCau);
    $("#ddlKhoXuat").selectpicker("val", dataPhieuYeuCauXuatVatTu[0].MaKhoXuat);
    $("#txtDienGiai").val(dataPhieuYeuCauXuatVatTu[0].DienGiai);
    loaddanhsachvattu_phieuyeucauxuatvattu(maphieuyeucauxuatvattu);
    $("#ul_tab_phieuyeucauxuatvattu a[href=\"#tab_phieuyeucauxuatvattu\"]").tab("show");
    $("#div_phieuyeucauxuatvattu").modal("show");
}
function luuphieuyeucauxuatvattu() {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#txtNgayYeuCau, #txtNguoiYeuCau, #ddlKhoYeuCau, #ddlKhoXuat").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    if (!$("#form_phieuyeucauxuatvattu_thongtinphieu").valid()) return;
    if (maphieuyeucauxuatvattu === "") {
        maphieuyeucauxuatvattu = PhieuYeuCauXuatVatTu_Insert(
            $("#ddlKhoYeuCau option:selected").val(), $("#txtNgayYeuCau").val(), $("#txtNguoiYeuCau").val(),
            $("#ddlKhoXuat option:selected").val(), $("#txtDienGiai").val(), username);
        if (maphieuyeucauxuatvattu === "") {
            $("#txtNguoiYeuCau").focus();
            return;
        }
    } else {
        PhieuYeuCauXuatVatTu_Update(
            $("#ddlKhoYeuCau option:selected").val(), maphieuyeucauxuatvattu, $("#txtNgayYeuCau").val(),
            $("#txtNguoiYeuCau").val(), $("#ddlKhoXuat option:selected").val(), $("#txtDienGiai").val(), username);
    }
    $("#btnNhapLaiPhieu").hide();
    $("#ul_tab_phieuyeucauxuatvattu a[href=\"#tab_phieuyeucauxuatvattu_chitiet\"]").tab("show");
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
}
function resetphieuyeucauxuatvattu() {
    ResetControlForm("#form_phieuyeucauxuatvattu_thongtinphieu");
    $("#txtNguoiYeuCau").focus();
}
function suaphieuyeucauxuatvattu_chitiet(tagInput) {
    if (CheckSession() === "") { window.location = "/"; return; }
    var data = PhieuYeuCauXuatVatTu_ChiTiet_SelectById($(tagInput).attr("maphieuyeucauxuatvattu_chitiet"));
    if (data.length <= 0) {
        bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        return;
    }
    $("#ul_tab_phieuyeucauxuatvattu a[href=\"#tab_phieuyeucauxuatvattu_chitiet\"]").tab("show");
    maphieuyeucauxuatvattu_chitiet = data[0].ID;
    $("#ddlVatTu").selectpicker("val", data[0].MaVatTu).change();
    $("#txtSoLuong").val(data[0].SoLuong);
}
function luuphieuyeucauxuatvattu_chitiet() {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#ddlVatTu, #txtSoLuong").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    if (!$("#form_phieuyeucauxuatvattu_chitietphieu").valid()) return;
    if (maphieuyeucauxuatvattu !== "") {
        if (maphieuyeucauxuatvattu_chitiet === "") {
            PhieuYeuCauXuatVatTu_ChiTiet_Insert(
                maphieuyeucauxuatvattu, $("#ddlVatTu option:selected").val(),
                $("#ddDonVi option:selected").val(), $("#txtSoLuong").val(), username
            );
        } else {
            PhieuYeuCauXuatVatTu_ChiTiet_Update(
                maphieuyeucauxuatvattu_chitiet, maphieuyeucauxuatvattu, $("#ddlVatTu option:selected").val(),
                $("#ddDonVi option:selected").val(), $("#txtSoLuong").val(), username
            );
        }
        loaddanhsachvattu_phieuyeucauxuatvattu(maphieuyeucauxuatvattu);
        resetphieuyeucauxuatvattuchitiet();
    } else {
        bootbox.alert("Bạn chưa tạo phiếu hủy, không thể thêm vật tư mới!");
        $("#txtNguoiGiao").focus();
    }
}
function xoaphieuyeucauxuatvattu_chitiet(tagInput, multiDelete) {
    if (CheckSession() === "") { window.location = "/"; return; }
    if (multiDelete) {
        var arrId = $("#tb_phieuyeucauxuatvattu_chitiet").bootstrapTable("getSelections");
        if (arrId.length > 0) {
            bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
                if (result) {
                    $.each(arrId, function (key, value) {
                        PhieuYeuCauXuatVatTu_ChiTiet_Delete(value[1]);
                    });
                    loaddanhsachvattu_phieuyeucauxuatvattu(maphieuyeucauxuatvattu);
                }
            });
        } else {
            bootbox.alert("Bạn cần chọn bản ghi trước khi xóa!");
        }
    } else {
        bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
            if (result) {
                PhieuYeuCauXuatVatTu_ChiTiet_Delete($(tagInput).attr("maphieuyeucauxuatvattu_chitiet"));
                loaddanhsachvattu_phieuyeucauxuatvattu(maphieuyeucauxuatvattu);
            }
        });
    }
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
}
function loaddanhsachvattu_phieuyeucauxuatvattu(maphieuyeucauxuatvattu) {
    var data = PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu(maphieuyeucauxuatvattu);
    $("#tb_phieuyeucauxuatvattu_chitiet").bootstrapTable("destroy");
    $("#tbody_phieuyeucauxuatvattu_chitiet").empty();
    var str = "";
    $.each(data, function (key, value) {
        str += "<tr>";
        str += "<td></td>";
        str += "<td>" + value.ID + "</td>";
        str += "<td>" + value.TenVatTu + "</td>";
        str += "<td>" + value.TenDonVi + "</td>";
        str += "<td>" + $.number(value.SoLuong, 0, ".", ",") + "</td>";
        str += "<td>" +
                    "<button type=\"button\" id=\"btnSuaPhieuYeuCauXuatVatTu_ChiTiet\" title=\"Sửa\" maphieuyeucauxuatvattu_chitiet=\"" + value.ID + "\" onclick=\"suaphieuyeucauxuatvattu_chitiet(this)\" class=\"btn btn-info\">" +
                        "<span class=\"glyphicon glyphicon-edit\"></span>" +
                    "</button>" +
                    "&nbsp;" +
                    "<button type=\"button\" id=\"btnXoaPhieuYeuCauXuatVatTu_ChiTiet\" title=\"Xóa\" maphieuyeucauxuatvattu_chitiet=\"" + value.ID + "\" onclick=\"xoaphieuyeucauxuatvattu_chitiet(this, false)\" class=\"btn btn-danger\">" +
                        "<span class=\"glyphicon glyphicon-trash\"></span>" +
                    "</button>" +
               "</td>";
        str += "</tr>";
    });
    $("#tbody_phieuyeucauxuatvattu_chitiet").append(str);
    $("#tb_phieuyeucauxuatvattu_chitiet").bootstrapTable({
        classes: "table table-condensed",
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        }
    });
}
function resetphieuyeucauxuatvattuchitiet() {
    ResetControlForm("#form_phieuyeucauxuatvattu_chitietphieu");
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
    maphieuyeucauxuatvattu_chitiet = "";
}
function viewphieuyeucauxuatvattu(tagButton) {
    maphieuyeucauxuatvattu = $(tagButton).attr("maphieu");
    if (maphieuyeucauxuatvattu === "") return;
    var dataPhieuYeuCauXuatVatTu = PhieuYeuCauXuatVatTu_SelectById(maphieuyeucauxuatvattu);
    (dataPhieuYeuCauXuatVatTu[0].NgayYeuCau == null || dataPhieuYeuCauXuatVatTu[0].NgayYeuCau === "")
        ? $("#txtNgayYeuCau_ViewPhieuYeuCauXuatVatTu").val("")
        : $("#txtNgayYeuCau_ViewPhieuYeuCauXuatVatTu").val(moment(dataPhieuYeuCauXuatVatTu[0].NgayYeuCau).format("DD-MM-YYYY"));
    $("#txtNguoiYeuCau_ViewPhieuYeuCauXuatVatTu").val(dataPhieuYeuCauXuatVatTu[0].NguoiYeuCau);
    $("#txtKhoYeuCau_ViewPhieuYeuCauXuatVatTu").val(dataPhieuYeuCauXuatVatTu[0].TenKhoYeuCau);
    $("#txtKhoXuat_ViewPhieuYeuCauXuatVatTu").val(dataPhieuYeuCauXuatVatTu[0].TenKhoXuat);
    $("#txtDienGiai_ViewPhieuYeuCauXuatVatTu").val(dataPhieuYeuCauXuatVatTu[0].DienGiai);
    // Thông tin thuốc-vật tư
    var data = PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu(maphieuyeucauxuatvattu);
    $("#tb_phieuyeucauxuatvattu_chitiet_view").bootstrapTable("destroy");
    $("#tbody_phieuyeucauxuatvattu_chitiet_view").empty();
    var str = "";
    $.each(data, function (key, value) {
        str += "<tr>";
        str += "<td>" + value.TenVatTu + "</td>";
        str += "<td>" + value.TenDonVi + "</td>";
        str += "<td>" + $.number(value.SoLuong, 0, ".", ",") + "</td>";
        str += "</tr>";
    });
    $("#tbody_phieuyeucauxuatvattu_chitiet_view").append(str);
    $("#tb_phieuyeucauxuatvattu_chitiet_view").bootstrapTable({
        classes: "table table-condensed",
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        }
    });
    $("#div_phieuyeucauxuatvattu_view").modal("show");
}
function PhieuYeuCauXuatVatTu_Insert(makhoyeucau, ngayyeucau, nguoiyeucau, makhoxuat, diengiai, nguoitao) {
    var maphieu = "";
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_Insert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makhoyeucau': '" + makhoyeucau + "', 'ngayyeucau': '" + ngayyeucau + "'" +
            ", 'nguoiyeucau': '" + nguoiyeucau + "', 'makhoxuat': '" + makhoxuat + "'" +
            ", 'diengiai': '" + diengiai + "', 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function (n) {
            maphieu = n.d;
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return maphieu;
}
function PhieuYeuCauXuatVatTu_Update(makhoyeucau, maphieu, ngayyeucau, nguoiyeucau, makhoxuat, diengiai, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_Update",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makhoyeucau': '" + makhoyeucau + "', 'maphieu': '" + maphieu + "', 'ngayyeucau': '" + ngayyeucau + "'" +
            ", 'nguoiyeucau': '" + nguoiyeucau + "' , 'makhoxuat': '" + makhoxuat + "'" +
            ", 'diengiai': '" + diengiai + "', 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuYeuCauXuatVatTu_SelectById(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_SelectById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuYeuCauXuatVatTu_SelectKhoXuat(makhoa) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_SelectKhoXuat",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makhoa': '" + makhoa + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuYeuCauXuatVatTu_ChiTiet_Insert(maphieu, mavattu, madonvi, soluong, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_ChiTiet_Insert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "', 'madonvi': '" + madonvi + "'" +
            ", 'soluong': " + parseInt(soluong) + ", 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuYeuCauXuatVatTu_ChiTiet_Update(id, maphieu, mavattu, madonvi, soluong, dongia, losanxuat, hansudung, maphieunhap, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_ChiTiet_Update",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id': '" + id + "', 'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "'" +
            ", 'madonvi': '" + madonvi + "', 'soluong': " + parseInt(soluong) + ", 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuYeuCauXuatVatTu_ChiTiet_SelectById(id) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_ChiTiet_SelectById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id': '" + id + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_ChiTiet_SelectByMaPhieu",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuYeuCauXuatVatTu_ChiTiet_Delete(id) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuYeuCauXuatVatTu.asmx/PhieuYeuCauXuatVatTu_ChiTiet_Delete",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id': '" + id + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}